package com.springdoor.common.utils;

import com.alibaba.fastjson2.JSONObject;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Component
@ConfigurationProperties(prefix = "qiniu")
public class QiNiuUtils {

    private String accessKey;

    private String secretKey;

    private String bucketName;

    private String url;

    public String uploadImageQiniu(MultipartFile multipartFile) {
        return uploadImageQiniu(multipartFile, null);
    }

    public String uploadImageAvatarQiniu(MultipartFile multipartFile) {
        return uploadImageQiniu(multipartFile, "avatar/");
    }

    /**
     * 上传图片到七牛云
     * @param multipartFile
     * @return
     */
    private String uploadImageQiniu(MultipartFile multipartFile, String affix) {
        try {
            if (StringUtils.isBlank(affix)) {
                affix = "upload/";
            }
            //1、获取文件上传的流
            byte[] fileBytes = multipartFile.getBytes();
            //2、创建日期目录分隔
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
            String datePath = dateFormat.format(new Date());

            //3、获取文件名
            String originalFilename = multipartFile.getOriginalFilename();
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            String filename = affix + datePath+"/"+ UUID.randomUUID().toString().replace("-", "")+ suffix;

            //4.构造一个带指定 Region 对象的配置类
            //Region根据自己的对象空间的地址选
            Configuration cfg = new Configuration(Region.region2());
            UploadManager uploadManager = new UploadManager(cfg);

            //5.获取七牛云提供的 token
            Auth auth = Auth.create(accessKey, secretKey);
            String upToken = auth.uploadToken(bucketName);
            System.out.println("token: " + upToken);
            Response response = uploadManager.put(fileBytes, filename, upToken);
            JSONObject jsonObject = JSONObject.parseObject(response.bodyString());
            String path = (String) jsonObject.get("key");

            return url + "/" + path;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String getAccessKey() {
        return accessKey;
    }

    public void setAccessKey(String accessKey) {
        this.accessKey = accessKey;
    }

    public String getSecretKey() {
        return secretKey;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }

    public String getBucketName() {
        return bucketName;
    }

    public void setBucketName(String bucketName) {
        this.bucketName = bucketName;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}
